package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.DUserMapper;
import com.ctgu.j08.service.DUserService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.DUser;
import com.ctgu.j08.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/5/25 15:03
 */
@Service
public class DUserServiceImpl extends ServiceImpl<DUserMapper,DUser> implements DUserService {
    @Autowired
    DUserMapper dUserMapper;
    @Override
    public DUser getById(int id) {
        return null;
    }

    @Override
    public DUser getByUsername(String username) {
        DUser dUser = dUserMapper.selectByUsername(username);
        return dUser;
    }

    @Override
    public Result editUser(DUser dUser) {
        Integer userId = dUser.getUserId();
        System.out.println("编辑用户id:"+userId);
        int isUp = dUserMapper.updateByPrimaryKey(dUser);
        if(isUp != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
        else
        {
            System.out.println("更新失败"+isUp);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result addUser(DUser dUser) {
        boolean isAdd = this.save(dUser);
        if(isAdd != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result delUserById(Integer userId) {
        int isDel = dUserMapper.deleteByPrimaryKey(userId);
//        boolean isDel = this.removeById(userId);
        if(isDel!=0)
        {
            System.out.println("删除成功："+userId);
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllUserInfo() {
        List<DUser> dUserList = dUserMapper.getAllUserInfo();
        if(dUserList == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }else
        {
            System.out.println("用户列表："+dUserList);
            return ResultUtil.success(dUserList);
        }
    }

    @Override
    public Result searchUserByName(String userName) {
        DUser dUser = dUserMapper.selectByUsername(userName);
        if(dUser != null)
        {
            System.out.println("搜索到的用户"+dUser);
            return ResultUtil.success(dUser);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

//    @Override
//    public Result getAllUserInfo(int page, int size) {
//        IPage<DUser> userInfoPages = this.page(new Page<>(page, size), null);
//        if(userInfoPages != null){
//            return ResultUtil.success(userInfoPages);
//        }else {
//            return ResultUtil.error(ResultEnum.FAIL);
//        }
//    }

}
